package cn.yutong.system.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;

import cn.yutong.framework.util.ExcelData;
import cn.yutong.framework.util.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping({ "/excel" })
@Api(tags = "excel导出")
public class ExportExcelController {
	@PostMapping({ "/export" })
	@ApiOperation("excel 导出相关请求")
	public void export(@ApiParam("文件名") @RequestParam("filename") String fileName,
			@ApiParam("表头名称(第一行表头)") @RequestParam("headtext") List<String> headText,
			@ApiParam("表格数据,多行数据逗号分隔[如:两列两行的数据格式为('a,b,c,d')]") @RequestParam("data") List data,
			@ApiParam("表格中的SheetName") String sheetName, HttpServletResponse response) throws Exception {
		ExcelData excelData = new ExcelData();
		excelData.setTitles(headText);
		List<List<Object>> excelRows = Lists.newArrayList();

		int colNum = headText.size();
		for (int i = 0; i < data.size() / colNum; i++) {
			List<Object> subList = data.subList(i, i + colNum);
			excelRows.add(subList);
		}
		excelData.setRows(excelRows);
		excelData.setSheetName(sheetName);
		ExcelUtil.exportExcel(response, fileName, excelData);
	}
}
